import "package:ct_pa_majordomo/config/roomIcons.dart";
import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/home/widgets/section.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:flutter/material.dart";

class Todo extends StatelessWidget {
  final Map params;
  
  const Todo({super.key, required this.params});

  @override
  Widget build(BuildContext context) {
    return Section(
      title: "待办事项",
      child: SizedBox(
        height: 220,  // 固定高度，避免布局计算问题
        child: Column(
          children: [
            Expanded(
              child: Row(
                children: [
                  Expanded(child: _buildBillCard()),
                  const SizedBox(width: 12),
                  Expanded(child: _buildPreCheckInCard()),
                ],
              ),
            ),
            const SizedBox(height: 12),
            Expanded(
              child: Row(
                children: [
                  Expanded(child: _buildOverdueCard()),
                  const SizedBox(width: 12),
                  Expanded(child: _buildRoomCard()),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBillCard() {
    return _TodoCard(
      title: "账单管理",
      icon: RoomIcons.custom_bill,
      value: "${params['pendingCount'] ?? 0}",
      label: "待处理账单",
      isLarge: false,
      onTap: () => Stacker.pushNamed(Keys.BILL_MANAGE),
    );
  }

  Widget _buildPreCheckInCard() {
    return _TodoCard(
      title: "预登记",
      icon: Icons.person_add,
      value: "${params['pendingRegisterCount'] ?? 0}",
      label: "待处理预登记",
      isLarge: false,
      onTap: () => Stacker.pushNamed(Keys.PRE_CHECK_IN),
    );
  } 

  Widget _buildOverdueCard() {
    return _TodoCard(
      title: "已逾期",
      icon: RoomIcons.custom_expiration,
      value: "¥${params['overdueAmount'] ?? 0}",
      label: "待处理金额",
      isLarge: false,
      onTap: () => Stacker.pushNamed(Keys.BILL, arguments: {"current": 0, "params": {"status": "2", "name": "逾期"}}),
      valueColor: ThemeVars.colorWarning,
    );
  }

  Widget _buildRoomCard() {
    return _TodoCard(
      title: "空房管理",
      icon: RoomIcons.custom_room_manage,
      value: "${params['emptyRoomCount'] ?? 0}",
      subValue: "/${params['totalRoomCount'] ?? 0}间",
      label: "空置房间",
      onTap: () => Stacker.pushNamed(Keys.MAIN, arguments: { "index": 1, "params": {"status": "empty", "label": "空房"}}),
    );
  }
}

class _TodoCard extends StatelessWidget {
  final String title;
  final IconData icon;
  final String value;
  final String? subValue;
  final String label;
  final VoidCallback onTap;
  final bool showButton;
  final bool isLarge;
  final Color? valueColor;

  const _TodoCard({
    required this.title,
    required this.icon,
    required this.value,
    this.subValue,
    required this.label,
    required this.onTap,
    this.showButton = false,
    this.isLarge = true,
    this.valueColor,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              offset: const Offset(0, 2),
              blurRadius: 8,
              spreadRadius: 0,
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Icon(
                  icon,
                  size: 16,
                  color: ThemeVars.colorPrimary,
                ),
                const SizedBox(width: 8),
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
            SizedBox(height: 6,),
            Expanded(
              child: Center(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.baseline,
                      textBaseline: TextBaseline.alphabetic,
                      children: [
                        Text(
                          value,
                          style: TextStyle(
                            color: valueColor ?? ThemeVars.colorPrimary,
                            fontSize: isLarge ? 28 : 18,
                            fontWeight: FontWeight.w600,
                            height: 1,
                          ),
                        ),
                        if (subValue != null)
                          Text(
                            subValue!,
                            style: TextStyle(
                              color: Colors.grey[600],
                              fontSize: 14,
                              fontWeight: FontWeight.normal,
                            ),
                          ),
                      ],
                    ),
                    // const SizedBox(height: 6),
                    Container(
                      padding: const EdgeInsets.only(top: 6),
                      child: Text(
                        label,
                        style: const TextStyle(
                          color: ThemeVars.colorTextTer,
                          fontSize: 12,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            if (showButton)
              Center(
                child: Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 16,
                    vertical: 8,
                  ),
                  decoration: BoxDecoration(
                    color: ThemeVars.colorPrimary.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "点击处理",
                        style: TextStyle(
                          color: ThemeVars.colorPrimary,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      const SizedBox(width: 4),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 12,
                        color: ThemeVars.colorPrimary,
                      ),
                    ],
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
